# -*- coding: UTF-8 -*-
# @Time : 2022-02-04 20:24
# @User：My.
# @File : BasePage.py
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from Utils.LogUtil import my_log
import allure
import datetime


# 定义类
class Action(object):
    def __init__(self, driver):
        self.driver = driver
        self.log = my_log('BasePage')

    def find_element(self, by, value, timeout=30, poll=3):
        try:
            WebDriverWait(self.driver, timeout, poll).until(lambda x: x.find_element(by, value))
            return self.driver.find_element(by, value)
        except Exception as e:
            self.log.debug("没有找到元素{}".format(e))

    def click_btn(self, **kwargs):

        global click
        by, value = kwargs['by'], kwargs['value']
        if by == 'id':
            click = self.by_id(value)
        elif by == 'xpath':
            click = self.by_xpath(value)
        click.click()

    def by_xpath(self, value):
        return self.find_element(By.XPATH, value)

    def by_id(self, value):
        return self.find_element(By.ID, value)

    def send_keys(self, **kwargs):
        # return self.find_element(By.XPATH, value).send_keys(send_value)
        global keys
        by, value = kwargs['by'], kwargs['value']
        if by == 'id':
            keys = self.by_id(value)
        elif by == 'xpath':
            keys = self.by_xpath(value)
        keys.click()
        keys.send_keys(kwargs['send'])

    def get_toase(self, **kwargs):
        # toase_send = "//*[contains(@text,'" + text + "')]"
        try:
            text = kwargs['expect']
            toase_send = "//*[text()='" + text + "']"
            toase = WebDriverWait(self.driver, timeout=30, poll_frequency=0.5).until(
                lambda x: x.find_element(By.XPATH, toase_send))
            # return toase.get_attribute('textContent')
            self.log.info('Toase获取信息{0}'.format(toase.get_attribute('textContent')))
            return True
        except Exception as e:
            self.log.debug('Toase获取失败，错误信息{0}'.format(e))
            return False

    def save_screenshot(self, file_path):
        return self.driver.save_scrrenshot(file_path)

    def assert_toase_result(self, **kwargs):
        toase_result = self.get_toase(**kwargs)
        assert toase_result == True
        """
        try:
            assert toase_result
        except Exception as e:
            png = self.driver.get_screenshot_as_png()
            allure.attach(png, 'toast错误', allure.attachment_type.PNG)
            raise e
"""


# 定义装饰器
def screenshot_allure(func):
    # 定义内部函数
    def get_err_screenshot(self, *args, **kwargs):
        # 定义内部函数 拍图
        try:
            func(self, *args, **kwargs)
        except Exception as e:
            png = self.driver.get_screenshot_as_png()
            name = datetime.datetime.now().strftime('%Y-%m-%d %H-%M-%S')
            allure.attach(png, name, allure.attachment_type.PNG)
            raise e

    # 返回内部函数名称
    return get_err_screenshot
    # 重构toase
    # 调用函数
